package tt.dz.biz.dao.user;

import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.stereotype.Repository;
import tt.dz.biz.dao.BaseDAO;
import tt.dz.common.LogUtil;
import tt.dz.vo.OnlineAccountVO;

@Repository("onlineAccountDAO")
public class OnlineAccountDAO
  extends BaseDAO
{
  public static final String QUERY_ONLINE_CARD = "select  moa.card_no,moa.user_id,moa.card_type,moa.card_bin,moa.area_code,moa.check_code,moa.state,moa.open_date,moa.end_date ,acct_bal,last_date from ms_online_account  moa ";
  private static final String INSERT_ONLINE_ACCOUNT = "insert into ms_online_account(card_no,user_id,card_type,card_bin,area_code,check_code,state,open_date,end_date)  values(:cardNo,:userId,:cardType,:cardBin,:areaCode,:checkCode,:state,:openDate,:endDate )";
  private static final String UPDATE_ONLINE_ACCOUNT_BAL = "update ms_online_account  set  acct_bal=acct_bal+:acctBal,last_date=:lastDate where card_no=:cardNo";
  private static final String QUERY_USERID_BY_CARDNO = "select user_id from ms_online_account where card_no = :cardNo";
  
  public OnlineAccountVO queryOnlineCardByNo(String cardNo)
    throws Exception
  {
    try
    {
      StringBuilder cardSql = new StringBuilder("select  moa.card_no,moa.user_id,moa.card_type,moa.card_bin,moa.area_code,moa.check_code,moa.state,moa.open_date,moa.end_date ,acct_bal,last_date from ms_online_account  moa ");
      cardSql.append(" where moa.card_no =:cardNo");
      OnlineAccountVO param = new OnlineAccountVO();
      param.setCardNo(cardNo);
      return (OnlineAccountVO)queryForObject(cardSql.toString(), param, OnlineAccountVO.class);
    }
    catch (EmptyResultDataAccessException e)
    {
      return null;
    }
    catch (Exception e)
    {
      LogUtil.error("通过卡号查询虚拟线上卡异常，参数为cardNo：" + cardNo, e);
      throw e;
    }
  }
  
  public OnlineAccountVO queryOnlineCardByUser(String userId)
    throws Exception
  {
    try
    {
      StringBuilder cardSql = new StringBuilder("select  moa.card_no,moa.user_id,moa.card_type,moa.card_bin,moa.area_code,moa.check_code,moa.state,moa.open_date,moa.end_date ,acct_bal,last_date from ms_online_account  moa ");
      cardSql.append(" where moa.user_id =:userId");
      OnlineAccountVO param = new OnlineAccountVO();
      param.setUserId(userId);
      return (OnlineAccountVO)queryForObject(cardSql.toString(), param, OnlineAccountVO.class);
    }
    catch (EmptyResultDataAccessException e)
    {
      return null;
    }
    catch (Exception e)
    {
      LogUtil.error("通过userId查询虚拟线上卡异常，参数为userId：" + userId, e);
      throw e;
    }
  }
  
  public int insertOnlineAccount(OnlineAccountVO onlineAccountVO)
  {
    return save("insert into ms_online_account(card_no,user_id,card_type,card_bin,area_code,check_code,state,open_date,end_date)  values(:cardNo,:userId,:cardType,:cardBin,:areaCode,:checkCode,:state,:openDate,:endDate )", onlineAccountVO);
  }
  
  public boolean updateOnlineAccountBal(OnlineAccountVO onlineAccountVO)
  {
    return 1 == save("update ms_online_account  set  acct_bal=acct_bal+:acctBal,last_date=:lastDate where card_no=:cardNo", onlineAccountVO);
  }
  
  public String queryUserIdByCardNo(String cardNo)
    throws Exception
  {
    try
    {
      OnlineAccountVO param = new OnlineAccountVO();
      param.setCardNo(cardNo);
      return (String)super.queryForObject("select user_id from ms_online_account where card_no = :cardNo", param, String.class);
    }
    catch (EmptyResultDataAccessException e)
    {
      return null;
    }
    catch (Exception e)
    {
      LogUtil.error("通过cardNo查询userId异常，参数为cardNo：" + cardNo, e);
      throw e;
    }
  }
}
